<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>字典项管理</title>
    <% include("../layout/css.html"){} %>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card card-box-shadow">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input name="name" value="" class="layui-input" type="text" placeholder="名称"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="searchSubmit" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="dictionarydataTable" lay-filter="dictionarydataTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelDictionarydata">
    <form id="modelDictionarydataForm" lay-filter="modelDictionarydataForm" class="layui-form model-form">
        <input id="id" name="id" type="hidden"/>
        <input id="unique" name="unique" type="hidden"/>
        
		<div class="layui-form-item">
			<label class="layui-form-label">字典项主键</label>
			<div class="layui-input-block">
			    <input  id="dictdata_code" name="dictdata_code"  type="text" class="layui-input"  autocomplete="off"
			           lay-verType="tips" lay-verify="required|dictdata_code_unique" placeholder="请输入字典项主键"/>
			</div>
		</div>
		<div class="layui-form-item" style="display: none;">
			<label class="layui-form-label">字典主键</label>
			<div class="layui-input-block">
			    <input  id="dict_code" name="dict_code"  type="text" class="layui-input"  autocomplete="off"
			           lay-verType="tips" lay-verify="" value="${dict_code}" placeholder="请输入字典主键"/>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">字典项值</label>
			<div class="layui-input-block">
			    <input  id="dictdata_name" name="dictdata_name"  type="text" class="layui-input"  autocomplete="off"
			           lay-verType="tips" lay-verify="" placeholder="请输入字典项值"/>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">排序</label>
			<div class="layui-input-block">
			    <input  id="sort_number" name="sort_number"  type="number" class="layui-input"  autocomplete="off"
                        lay-verType="tips" lay-verify="number" placeholder="请输入排序"/>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">描述</label>
			<div class="layui-input-block">
			    <input  id="description" name="description"  type="text" class="layui-input"  autocomplete="off"
			           lay-verType="tips" lay-verify="" placeholder="请输入描述"/>
			</div>
		</div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closePageDialog">取消</button>
            <button class="layui-btn" lay-filter="modelDictionarydataSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<% include("../layout/js.html"){} %>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'zTree', 'tableX','laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var tableX = layui.tableX;
        var laydate = layui.laydate;

        

        // 渲染表格
        var insTb = tableX.render({
            elem: '#dictionarydataTable',
            url: 'dictionarydata/list',
            page: true,
            toolbar: true,
            cellMinWidth: 200,
            cols: [[
                {type: 'numbers'}
				,{field: "dict_name", minWidth: "175", title: "字典主键", sort: true, hide: false}
                ,{field: "dictdata_code", minWidth: "270", title: "字典项主键", sort: true, hide: false}
				,{field: "dictdata_name", minWidth: "160", title: "字典项值", sort: true, hide: false}
				,{field: "sort_number", minWidth: "80", title: "排序", sort: true, hide: false}
				,{field: "description", minWidth: "80", title: "描述", sort: true, hide: false}
				,{field: "createTime", minWidth: "80", title: "创建时间", sort: true, hide: true, templet: function (d) {
                    return util.toDateString(d.createTime);
                }}
				,{field: "createUser", minWidth: "80", title: "创建人", sort: true, hide: true}
				,{field: "updateTime", minWidth: "80", title: "更新时间", sort: true, hide: true, templet: function (d) {
                    return util.toDateString(d.createTime);
                }}
				,{field: "updateUser", minWidth: "80", title: "更新人", sort: true, hide: true}
		        ,{align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200, fixed: 'right'}
            ]],
            where:{"dict_code":"${dict_code}"}
        });

        // 添加
        $('#btnAdd').click(function () {
            showEditModel();
        });

        // 搜索
        form.on('submit(searchSubmit)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
        });

        // 工具条点击事件
        table.on('tool(dictionarydataTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(obj);
            }
        });

        // 删除
        function doDel(obj) {
            layer.confirm('确定要删除“' + obj.data.dictdata_name + '”字典项吗？', {
                shade: .1,
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                $.post('dictionarydata/delete', {
                    ids: obj.data.id
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload();
                        //obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        // 显示编辑弹窗
        function showEditModel(mDictionarydata) {
            admin.open({
                type: 1,
                title: (mDictionarydata ? '修改' : '添加') + '字典项',
                content: $('#modelDictionarydata').html(),
                success: function (layero, dIndex) {
                    form.verify({
                        dictdata_code_unique: function(value) {
                            if($("#unique").val()!="canuse" && $("#unique").val()!=""){
                                return $("#unique").val();
                            }
                        }
                    });
                    $("#dictdata_code").keyup(function(){
                        $("button[lay-filter=modelDictionarySubmit]").attr({"disabled":"disabled"});
                        $.ajax({
                            url: 'dictionarydata/dictdata_code_unique',// 这里写你要验证的地址哦。
                            type:"post",
                            async: true, // 使用同步的方法
                            data: { //要提交到服务端验证的用户名
                                dictdata_code: $("#dictdata_code").val(),
                                id:$("#id").val()
                            },
                            dataType: 'json',
                            success: function(result) { //栗子，返回的数据结果：{"rel":true,"message":"用户名已存在！"}
                                if (result.code==500) {
                                    $("#unique").val(result.msg);
                                } else {
                                    $("#unique").val("canuse");
                                }
                                $("button[lay-filter=modelDictionarySubmit]").removeAttr("disabled");
                            }
                        });

                    });

                    var url = mDictionarydata ? 'dictionarydata/update' : 'dictionarydata/add';
                    form.val('modelDictionarydataForm', mDictionarydata);  // 回显数据
                    // 表单提交事件
                    form.on('submit(modelDictionarydataSubmit)', function (data) {
                        layer.load(2);
                        $.post(url, data.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload();
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }

    });
</script>
</body>
</html>
